package com.dev.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dev.bbs.consts.ArticleCate;
import com.dev.bbs.domain.BbsArticle;
import com.dev.bbs.domain.BbsUser;
import com.dev.bbs.service.IBbsArticleService;
import com.dev.bbs.service.IBbsUserCollectService;
import com.dev.bbs.service.IEsArticleService;
import com.dev.common.annotation.Log;
import com.dev.common.core.controller.BaseController;
import com.dev.common.core.domain.AjaxResult;
import com.dev.common.core.page.TableDataInfo;
import com.dev.common.enums.BusinessType;
import com.dev.common.exception.BusinessException;
import com.dev.common.utils.StringUtils;
import com.dev.common.utils.poi.ExcelUtil;
import com.dev.framework.util.ShiroUtils;

/**
 * 社区文章Controller
 *
 * @author ruoyi
 * @date 2019-09-17
 */
@Controller
@RequestMapping("article")
public class BbsArticleController extends BaseController
{
    private String                      prefix = "article";

    private static Map<String, Integer> cates;
    static
    {
        cates = new HashMap<String, Integer>();
        cates.put("ask", ArticleCate.ASK.getCode());
        cates.put("share", ArticleCate.SHARE.getCode());
        cates.put("discuss", ArticleCate.DISCUSS.getCode());
        cates.put("suggest", ArticleCate.SUGGEST.getCode());
    }

    @Autowired
    private IBbsArticleService     bbsArticleService;

    @Autowired
    private IBbsUserCollectService bbsUserCollectService;

    @Autowired
    private IEsArticleService      esArticleService;

    @GetMapping("{cateKey}/{artType}")
    public String articel(@PathVariable(value = "cateKey") String cateKey,
            @PathVariable(value = "artType") String artType, Model model)
    {
        Integer cate = cates.get(cateKey);
        model.addAttribute("cate", cate);
        model.addAttribute("cateKey", cateKey);
        model.addAttribute("artType", artType);
        return prefix + "/article";
    }

    @GetMapping("{cateKey}")
    public String articel(@PathVariable(value = "cateKey") String cateKey, Model model)
    {
        Integer cate = cates.get(cateKey);
        model.addAttribute("cate", cate);
        model.addAttribute("cateKey", cateKey);
        return prefix + "/article";
    }

    /**
    * 查询社区文章列表
    */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BbsArticle bbsArticle)
    {
        startPage();
        bbsArticle.setDelFlag(false);
        List<BbsArticle> list = bbsArticleService.selectBbsArticleList(bbsArticle);
        return getDataTable(list);
    }

    /**
     * 全文搜索社区文章
     */
    @PostMapping("/es/search/list")
    @ResponseBody
    public AjaxResult searchList(String keyword, Integer pageNum, Integer pageSize, Model model)
    {
        if (StringUtils.isEmpty(keyword))
        {
            return AjaxResult.error("请输入关键词");
        }
        // 分页
        if (null == pageNum || pageNum < 0)
        {
            pageNum = 0;
        }
        if (pageNum > 0)
        {
            pageNum = pageNum - 1;
        }
        if (null == pageSize || pageSize <= 0)
        {
            pageSize = 10;
        }
        Page<BbsArticle> page = esArticleService.selectBbsArticleEsList(keyword, pageNum, pageSize);
        return AjaxResult.success(page);
    }

    /**
     * 全文搜索社区文章
     */
    @GetMapping("/es/search")
    public String search(String keyword, Model model)
    {
        if (StringUtils.isEmpty(keyword))
        {
            throw new BusinessException("请输入搜索关键词！！！！！");
        }
        model.addAttribute("keyword", keyword);
        return prefix + "/search";
    }

    /**
     * 导出社区文章列表
     */
    @RequiresPermissions("bbs:article:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BbsArticle bbsArticle)
    {
        List<BbsArticle> list = bbsArticleService.selectBbsArticleList(bbsArticle);
        ExcelUtil<BbsArticle> util = new ExcelUtil<BbsArticle>(BbsArticle.class);
        return util.exportExcel(list, "article");
    }

    /**
     * 新增保存社区文章
     */
    // @RequiresPermissions("bbs:article:add")
    @Log(title = "社区文章", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BbsArticle bbsArticle)
    {
        BbsUser bbsUser = ShiroUtils.getBbsUser();
        bbsArticle.setAuthor(bbsUser.getUserId()).setCreateBy(bbsUser.getNickname());
        return toAjax(bbsArticleService.insertBbsArticle(bbsArticle)).put("artId", bbsArticle.getId());
    }

    /**
     * 修改社区文章
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        BbsArticle bbsArticle = bbsArticleService.selectBbsArticleById(id);
        mmap.put("bbsArticle", bbsArticle);
        return prefix + "/edit";
    }

    /**
     * 社区文章详情
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, Model model)
    {
        boolean collected = false;
        BbsArticle bbsArticle = bbsArticleService.selectBbsArticleById(id);
        BbsUser bbsUser = ShiroUtils.getBbsUser();
        if (null != bbsUser)
        {
            collected = bbsUserCollectService.collected(bbsUser.getUserId(), id);
        }
        model.addAttribute("collected", collected);
        model.addAttribute("bbsArticle", bbsArticle);
        bbsArticleService.view(id);
        return prefix + "/detail";
    }
}
